﻿namespace = shogunate_seppuku

# Commit a Seppuku in a siege
# based on siege.0001

scripted_trigger shogunate_seppuku_0001_can_seppuku = {
	# Characters must be located in the raided barony itself.
	location = scope:barony.title_province

	# Reasons to exclude characters from the Seppuku pool:
	NOR = {
		# If the raiders have defeated the holder's army, but the army has not yet retreated to a different province, shield them from Seppuku by the raid.
		exists = commanding_army
		exists = knight_army

		# Is imprisoned in the barony stay prisoners (special case, any courtiers/vassals of the raider will get released elsewhere).
		is_imprisoned = yes

		# Only capable adult commits Seppuku
		is_capable_adult = no

		# Only Samurai commits Seppuku
		NOT = { exists = dynasty }
		shogunate_has_samurai_culture_trigger = no

		# Only Shinto religion follower commits Seppuku
		NOT = { faith.religion = religion:shinto_religion }
	}
}

scripted_trigger shogunate_seppuku_0001_valid_domicile_trigger = {
	is_domicile_type = estate
	owner = { save_temporary_scope_as = estate_owner_check }
	scope:occupant = {
		OR = {
			any_war_enemy = { this = scope:estate_owner_check }
			any_war_enemy = { any_vassal_or_below = { this = scope:estate_owner_check } }
		}
	}
}

# Commit a Seppuku in a siege (trigger)
shogunate_seppuku.0001 = {
	hidden = yes

	trigger = {
		shogunate_after_kamakura_period_trigger = yes
		OR = {
			AND = {
				scope:county.holder = scope:previous_controller
				OR = {
					any_war_enemy = { this = scope:previous_controller }
#					any_war_enemy = { any_vassal_or_below = { this = scope:previous_controller } }
				}
			}
			AND = {
				scope:barony.title_province = {
					any_province_domicile = {
						is_domicile_type = estate
						owner = {
							save_temporary_scope_as = estate_owner_check
						}
					}
				}
				OR = {
					any_war_enemy = { this = scope:estate_owner_check }
#					any_war_enemy = { any_vassal_or_below = { this = scope:estate_owner_check } }
				}
			}
		}
	}

	immediate = {
		save_scope_as = occupant
		scope:barony.holder = { save_scope_as = holder }
		scope:barony.title_province = {
			random_province_domicile = {
				limit = {
					shogunate_seppuku_0001_valid_domicile_trigger = yes
				}
				owner ?= {
					save_scope_as = estate_owner
				}
			}
		}

		# Depending on if the occupier is an attacker or defender in this war, get the primary attacker/defender of the siege.
		scope:war = {
			if = {
				limit = {
					is_attacker = scope:occupant
				}
				# If occupier is an attacker, the war's primary attacker is also the primary attacker in this siege (the attackers have occupied one of the defender's provinces).
				primary_attacker = {
					save_scope_as = primary_siege_attacker
				}
				primary_defender = {
					save_scope_as = primary_siege_defender
				}
			}
			else = {
				# Otherwise the occupier is a defender, so the primary defender in the war is the primary attacker in the siege (the defenders have occupied one of the attacker's provinces).
				primary_defender = {
					save_scope_as = primary_siege_attacker
				}
				primary_attacker = {
					save_scope_as = primary_siege_defender
				}
			}
		}
		
		###BUILD SEPPUKU ATTEMPT LIST###
		#Add seppuku targets from all occupied baronies if it is someone's capital
		every_in_list = {
			list = occupied_baronies
			limit = {
				exists = holder
				save_temporary_scope_as = list_barony
				holder = { capital_barony = { this = scope:list_barony } }
			}
			holder = {
				if = {
					limit = { shogunate_seppuku_0001_can_seppuku = yes }
					add_to_list = seppuku_attempt_targets
				}
				every_courtier_or_guest = {
					limit = { shogunate_seppuku_0001_can_seppuku = yes }
					add_to_list = seppuku_attempt_targets
				}
			}
		}
		#Add capture targets from all hostile occupied Estates
		every_in_list = {
			list = occupied_baronies
			limit = {
				title_province = {
					any_province_domicile = {
						shogunate_seppuku_0001_valid_domicile_trigger = yes
					}
				}
			}
			title_province = {
				random_province_domicile = {
					limit = {
						shogunate_seppuku_0001_valid_domicile_trigger = yes
					}
					owner ?= {
						if = {
							limit = { shogunate_seppuku_0001_can_seppuku = yes }
							add_to_list = seppuku_attempt_targets
						}
						every_courtier_or_guest = {
							limit = { shogunate_seppuku_0001_can_seppuku = yes }
							add_to_list = seppuku_attempt_targets
						}
					}
				}
			}
		}

		###ATTEMPT SEPPUKU###
		scope:holder.capital_barony = { save_scope_as = capital }
		every_in_list = {
			list = seppuku_attempt_targets
			save_scope_as = actor
			if = {
				limit = {
					is_ai = no
					has_multiple_players = no
				}
				trigger_event = shogunate_seppuku.0002
			}
			else = {
				trigger_event = shogunate_seppuku.0003
			}
		}
	}
}

# Commit a Seppuku in a siege (single player)
shogunate_seppuku.0002 = {
	type = character_event

	title = shogunate_seppuku.0002.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:holder = scope:actor
				}
				desc = shogunate_seppuku.0002.desc.1
			}
			desc = shogunate_seppuku.0002.desc.2
		}
	}

	theme = faith
	left_portrait = {
		character = scope:actor
		animation = inspect_weapon # throne_room_ruler
	}
	override_background = { reference = shogunate_burning_castle }

	immediate = {
		if = {
			limit = {
				scope:holder = scope:actor
			}
			every_courtier = {
				limit = {
					NOT = { is_in_list = supporters }
					exists = location
					location = scope:actor.location
					is_male = yes
					is_imprisoned = no
					is_capable_adult = yes
					exists = dynasty
					shogunate_has_samurai_culture_trigger = yes
					faith.religion = religion:shinto_religion
				}
				add_to_list = supporters
			}
			if = {
				limit = {
					list_size = {
						name = supporters
						value >= 1
					}
				}
				ordered_in_list = {
					list = supporters
					order_by = {
						add = calc_rough_root_opinion_value
					}
					position = 0
					save_scope_as = supporter1
				}
			}
			if = {
				limit = {
					list_size = {
						name = supporters
						value >= 2
					}
				}
				ordered_in_list = {
					list = supporters
					order_by = {
						add = calc_rough_root_opinion_value
					}
					position = 1
					save_scope_as = supporter2
				}
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.a0

		trigger = {
			is_imprisoned = no
			NOT = { has_trait = poet }
			list_size = {
				name = supporters
				value = 0
			}
		}

		dynasty ?= {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
		death = {
			death_reason = death_seppuku
		}

		hidden_effect = {
			every_player = {
				limit = {
					NOT = { this = scope:actor }
				}
				trigger_event = shogunate_seppuku.0005
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.a1

		trigger = {
			is_imprisoned = no
			NOT = { has_trait = poet }
			list_size = {
				name = supporters
				value >= 1
			}
		}

		dynasty ?= {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
		death = {
			death_reason = death_seppuku
			killer = scope:supporter1
		}

		hidden_effect = {
			every_player = {
				limit = {
					NOT = { this = scope:actor }
				}
				trigger_event = shogunate_seppuku.0005
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.a2

		trigger = {
			is_imprisoned = no
			NOT = { has_trait = poet }
			list_size = {
				name = supporters
				value >= 2
			}
		}

		dynasty ?= {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
		death = {
			death_reason = death_seppuku
			killer = scope:supporter2
		}

		hidden_effect = {
			every_player = {
				limit = {
					NOT = { this = scope:actor }
				}
				trigger_event = shogunate_seppuku.0005
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.b0

		trigger = {
			is_imprisoned = no
			has_trait = poet
			list_size = {
				name = supporters
				value = 0
			}
		}

		dynasty ?= {
			add_dynasty_prestige = major_dynasty_prestige_gain
		}
		death = {
			death_reason = death_seppuku_with_poem
		}

		hidden_effect = {
			every_player = {
				limit = {
					NOT = { this = scope:actor }
				}
				trigger_event = shogunate_seppuku.0005
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.b1

		trigger = {
			is_imprisoned = no
			has_trait = poet
			list_size = {
				name = supporters
				value >= 1
			}
		}

		dynasty ?= {
			add_dynasty_prestige = major_dynasty_prestige_gain
		}
		death = {
			death_reason = death_seppuku_with_poem
			killer = scope:supporter1
		}

		hidden_effect = {
			every_player = {
				limit = {
					NOT = { this = scope:actor }
				}
				trigger_event = shogunate_seppuku.0005
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.b2

		trigger = {
			is_imprisoned = no
			has_trait = poet
			list_size = {
				name = supporters
				value >= 2
			}
		}

		dynasty ?= {
			add_dynasty_prestige = major_dynasty_prestige_gain
		}
		death = {
			death_reason = death_seppuku_with_poem
			killer = scope:supporter2
		}

		hidden_effect = {
			every_player = {
				limit = {
					NOT = { this = scope:actor }
				}
				trigger_event = shogunate_seppuku.0005
			}
		}
	}

	option = {
		name = shogunate_seppuku.0002.c

		trigger = {
			is_imprisoned = no
			OR = {
				has_trait = ambitious
				has_trait = vengeful
				has_trait = patient
			}
		}

		stress_impact = {
			brave     = major_stress_impact_gain
			stubborn  = medium_stress_impact_gain
			impatient = medium_stress_impact_gain
			just      = minor_stress_impact_gain
			zealous   = minor_stress_impact_gain
		}
	}

	option = {
		name = shogunate_seppuku.0002.d

		trigger = {
			is_imprisoned = no
			OR = {
				has_trait = arbitrary
				has_trait = cynical
			}
		}

		stress_impact = {
			brave     = major_stress_impact_gain
			stubborn  = medium_stress_impact_gain
			impatient = medium_stress_impact_gain
			just      = minor_stress_impact_gain
			zealous   = minor_stress_impact_gain
		}
	}

	option = {
		name = shogunate_seppuku.0002.e

		trigger = {
			is_imprisoned = no
			OR = {
				has_trait = craven
				has_trait = lazy
			}
		}

		stress_impact = {
			brave     = major_stress_impact_gain
			stubborn  = medium_stress_impact_gain
			impatient = medium_stress_impact_gain
			just      = minor_stress_impact_gain
			zealous   = minor_stress_impact_gain
		}
	}

	option = {
		name = shogunate_seppuku.0002.f

		trigger = {
			OR = {
				is_imprisoned = yes
				AND = {
					NOT = { has_trait = ambitious }
					NOT = { has_trait = vengeful }
					NOT = { has_trait = patient }
					NOT = { has_trait = arbitrary }
					NOT = { has_trait = cynical }
					NOT = { has_trait = craven }
					NOT = { has_trait = lazy }
				}
			}
		}

		stress_impact = {
			base      = medium_stress_impact_gain
			brave     = major_stress_impact_gain
			stubborn  = medium_stress_impact_gain
			impatient = medium_stress_impact_gain
			just      = minor_stress_impact_gain
			zealous   = minor_stress_impact_gain
		}
	}
}

# Commit a Seppuku in a siege (NPC, multiplayer)
shogunate_seppuku.0003 = {
	hidden = yes

	immediate = {
		random = {
			chance = shogunate_commit_seppuku_chance
			save_scope_value_as = {
				name = commit_seppuku
				value = yes
			}
		}

		if = {
			limit = {
				exists = scope:commit_seppuku
			}
			trigger_event = shogunate_seppuku.0004
		}
		else = {
			trigger_event = shogunate_seppuku.0005
		}
	}
}

# Commit a Seppuku in a siege - succeeded
shogunate_seppuku.0004 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				scope:holder = scope:actor
			}
			every_courtier = {
				limit = {
					NOT = { is_in_list = supporters }
					exists = location
					location = scope:actor.location
					is_male = yes
					is_imprisoned = no
					is_capable_adult = yes
					exists = dynasty
					shogunate_has_samurai_culture_trigger = yes
					faith.religion = religion:shinto_religion
				}
				add_to_list = supporters
			}
			if = {
				limit = {
					list_size = {
						name = supporters
						value >= 1
					}
				}
				ordered_in_list = {
					list = supporters
					order_by = {
						add = calc_rough_root_opinion_value
					}
					position = 0
					save_scope_as = supporter
				}
			}
		}

		if = {
			limit = {
				has_trait = poet
			}
			dynasty ?= {
				add_dynasty_prestige = major_dynasty_prestige_gain
			}
			if = {
				limit = {
					exists = scope:supporter
				}
				death = {
					death_reason = death_seppuku_with_poem
					killer = scope:supporter
				}
			}
			else = {
				death = {
					death_reason = death_seppuku_with_poem
				}
			}
		}
		else = {
			dynasty ?= {
				add_dynasty_prestige = medium_dynasty_prestige_gain
			}
			if = {
				limit = {
					exists = scope:supporter
				}
				death = {
					death_reason = death_seppuku
					killer = scope:supporter
				}
			}
			else = {
				death = {
					death_reason = death_seppuku
				}
			}
		}

		every_player = {
			save_scope_as = recipient
			if = {
				limit = {
					OR = {
						scope:actor = { is_ai = no }
						scope:actor = { has_any_relation_trigger = { CHARACTER = scope:recipient } }
						scope:recipient = scope:occupant
						scope:recipient = { any_pinned_character = { this = scope:actor } }
					}
				}
				trigger_event = shogunate_seppuku.0006
			}
		}
	}
}

# Commit a Seppuku in a siege - failed
shogunate_seppuku.0005 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				NOT = { has_trait = ambitious }
				NOT = { has_trait = vengeful }
				NOT = { has_trait = patient }
				NOT = { has_trait = arbitrary }
				NOT = { has_trait = cynical }
				NOT = { has_trait = craven }
				NOT = { has_trait = lazy }
			}
			stress_impact = {
				base = medium_stress_impact_gain
			}
		}
		stress_impact = {
			brave     = major_stress_impact_gain
			stubborn  = medium_stress_impact_gain
			impatient = medium_stress_impact_gain
			just      = minor_stress_impact_gain
			zealous   = minor_stress_impact_gain
		}
	}
}

# Commit a Seppuku in a siege (notify)
shogunate_seppuku.0006 = {
	type = character_event

	title = shogunate_seppuku.0006.t
	desc = {
		desc = shogunate_seppuku.0006.desc.1
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:actor = { has_trait = poet }
					exists = scope:supporter
				}
				desc = shogunate_seppuku.0006.desc.2.a
			}
			triggered_desc = {
				trigger = {
					scope:actor = { has_trait = poet }
					NOT = { exists = scope:supporter }
				}
				desc = shogunate_seppuku.0006.desc.2.b
			}
			triggered_desc = {
				trigger = {
					exists = scope:supporter
				}
				desc = shogunate_seppuku.0006.desc.2.c
			}
			desc = shogunate_seppuku.0006.desc.2.d
		}
	}

	theme = faith
	left_portrait = {
		character = scope:actor
		animation = inspect_weapon # throne_room_ruler
	}
	override_background = { reference = shogunate_burning_castle }

	option = {
		name = shogunate_seppuku.0006.a

		trigger = {
			NOT = { has_trait = arbitrary }
			NOT = { has_trait = cynical }
			NOT = { has_trait = craven }
			NOT = { has_trait = lazy }
			NOT = { has_trait = compassionate }
			NOT = { faith.religion = religion:christianity_religion }
		}
	}

	option = {
		name = shogunate_seppuku.0006.b

		trigger = {
			OR = {
				has_trait = arbitrary
				has_trait = cynical
			}
			NOT = { faith.religion = religion:christianity_religion }
		}
	}

	option = {
		name = shogunate_seppuku.0006.c

		trigger = {
			OR = {
				has_trait = craven
				has_trait = lazy
			}
			NOT = { faith.religion = religion:christianity_religion }
		}
	}

	option = {
		name = shogunate_seppuku.0006.d

		trigger = {
			has_trait = compassionate
			NOT = { faith.religion = religion:christianity_religion }
		}
	}

	option = {
		name = shogunate_seppuku.0006.e

		trigger = {
			faith.religion = religion:christianity_religion
		}
	}
}
